package cn.gdk.gaodekecc.util;




import cn.gdk.gaodekecc.exception.GlobalException;
import cn.gdk.gaodekecc.exception.GlobalExceptionCodeEnum;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AssertUtil {
    public static final String PHONE_REGEX = "^((13[0-9])|(14(0|[5-7]|9))|(15([0-3]|[5-9]))|(16(2|[5-7]))|(17[0-8])|(18[0-9])|(19([0-3]|[5-9])))\\d{8}$";

    //手机的正则表达式
    public static final Pattern CHINA_PATTERN_PHONE = Pattern.compile("^((13[0-9])|(14[0,1,4-9])|(15[0-3,5-9])|(16[2,5,6,7])|(17[0-8])|(18[0-9])|(19[0-3,5-9]))\\d{8}$");

    /**
     * --------------------------------------------------------
     * 手机号断言
     * --------------------------------------------------------
     **/
    public static void isPhone(String phone, GlobalExceptionCodeEnum globalExceptionCodeEnum) {
        isNotEmpty(phone, GlobalExceptionCodeEnum.COMM_PHONE_IS_NULL_ERROR);
        Matcher m = CHINA_PATTERN_PHONE.matcher(phone);
        if (!m.matches()) {
            throw new GlobalException(globalExceptionCodeEnum);
        }
    }


    /**
     * --------------------------------------------------------
     * 断言 不为空，如果为空，抛异常
     * --------------------------------------------------------
     **/
    public static void isNotEmpty(String text, GlobalExceptionCodeEnum globalExceptionCodeEnum) {
        if (text == null || text.trim().length() == 0) {
            throw new GlobalException(globalExceptionCodeEnum);
        }
    }


    /**
     * --------------------------------------------------------
     * 断言对象为空
     * --------------------------------------------------------
     **/
    public static void isNull(Object obj, GlobalExceptionCodeEnum globalExceptionCodeEnum) {
        if (obj != null) {
            throw new GlobalException(globalExceptionCodeEnum);


        }
    }

    public static void isNotNull(Object obj, GlobalExceptionCodeEnum globalExceptionCodeEnum) {
        if (obj == null) {
            throw new GlobalException(globalExceptionCodeEnum);


        }
    }

    /**
     * --------------------------------------------------------
     * 断言false,如果为true,我报错
     * --------------------------------------------------------
     **/
    public static void isFalse(boolean isFalse, GlobalExceptionCodeEnum globalExceptionCodeEnum) {
        if (isFalse) {
            throw new GlobalException(globalExceptionCodeEnum);


        }
    }

    public static void isTrue(boolean isTrue, GlobalExceptionCodeEnum globalExceptionCodeEnum) {
        if (!isTrue) {
            throw new GlobalException(globalExceptionCodeEnum);


        }
    }


    /**
     * --------------------------------------------------------
     * 断言两个字符串一致
     * --------------------------------------------------------
     **/
    public static void isEquals(String s1, String s2, GlobalExceptionCodeEnum globalExceptionCodeEnum) {
        isNotEmpty(s1, GlobalExceptionCodeEnum.COMMON_PARAM_IS_NULL_ERROR);
        isNotEmpty(s2, GlobalExceptionCodeEnum.COMMON_PARAM_IS_NULL_ERROR);
        if (!s1.equals(s2)) {
            throw new GlobalException(globalExceptionCodeEnum);


        }
    }

    public static void isEqualsTrim(String s1, String s2, GlobalExceptionCodeEnum globalExceptionCodeEnum) {
        isNotEmpty(s1, GlobalExceptionCodeEnum.COMMON_PARAM_IS_NULL_ERROR);
        isNotEmpty(s2, GlobalExceptionCodeEnum.COMMON_PARAM_IS_NULL_ERROR);
        if (!s1.trim().equals(s2.trim())) {
            throw new GlobalException(globalExceptionCodeEnum);


        }
    }

    public static void isEqualsIgnoreCase(String s1, String s2, GlobalExceptionCodeEnum globalExceptionCodeEnum) {
        isNotEmpty(s1, GlobalExceptionCodeEnum.COMMON_PARAM_IS_NULL_ERROR);
        isNotEmpty(s2, GlobalExceptionCodeEnum.COMMON_PARAM_IS_NULL_ERROR);
        if (!s1.trim().equalsIgnoreCase(s2.trim())) {
            throw new GlobalException(globalExceptionCodeEnum);


        }
    }

}
